package com.kbbywyclient.util.pre;

import android.content.Context;

import com.kbbywyclient.data.entity.User;


/**
 * Created by FHW
 * on 2017/3/30
 * Description: Preferences工具类
 */
public class PreUtils {
    /**
     * 是否登录
     *
     * @param context
     * @param loginFlag
     */
    public final static void setLogin(Context context, boolean loginFlag) {
        PreferencesUtils.putBoolean(context, PreKey.USER_LOGIN, loginFlag);
    }

    /**
     * 设置是否登录
     *
     * @param context
     * @return
     */
    public final static boolean isLogin(Context context) {
        return PreferencesUtils.getBoolean(context, PreKey.USER_LOGIN);
    }

    /**
     * 是否显示引导
     *
     * @param context
     * @return
     */
    public final static boolean isGuide(Context context) {
        return PreferencesUtils.getBoolean(context, PreKey.GUIDE_FLAG);
    }

    /**
     * 设置是否显示引导
     *
     * @param context
     * @param flag
     */
    public final static void setGuide(Context context, boolean flag) {
        PreferencesUtils.putBoolean(context, PreKey.GUIDE_FLAG, flag);
    }


    /**
     * 保存版本号
     *
     * @param context
     * @return
     */
    public final static int getVersionCode(Context context) {
        return PreferencesUtils.getInt(context, PreKey.VERSION_CODE);
    }

    /**
     * 设置是否显示引导
     *
     * @param context
     */
    public final static void setVersionCode(Context context, int code) {
        PreferencesUtils.putInt(context, PreKey.VERSION_CODE, code);
    }

    /**
     * 设置 记住密码
     *
     * @param context
     * @param remember
     */
    public final static void setRemember(Context context, boolean remember) {
        PreferencesUtils.putBoolean(context, PreKey.USER_REMEMBER, remember);
    }

    /**
     * 是否记住密码
     *
     * @param context
     * @return
     */
    public final static boolean isRemember(Context context) {
        return PreferencesUtils.getBoolean(context, PreKey.USER_REMEMBER, false);
    }

    public final static void setUserAccount(Context context, String account) {
        PreferencesUtils.putString(context, PreKey.USER_ACCOUNT, account);
    }

    public final static String getUserAccount(Context context) {
        return PreferencesUtils.getString(context, PreKey.USER_ACCOUNT, "");
    }

    public final static void setUserPwd(Context context, String pwd) {
        PreferencesUtils.putString(context, PreKey.USER_PWD, pwd);
    }

    public final static String getUserPwd(Context context) {
        return PreferencesUtils.getString(context, PreKey.USER_PWD, "");
    }

    public final static void setBusinessId(Context context, int type) {
        PreferencesUtils.putInt(context, PreKey.BUSINESS_ID, type);
    }

    public final static int getBusinessId(Context context) {
        return PreferencesUtils.getInt(context, PreKey.BUSINESS_ID, 0);
    }


    public final static void setCompanyID(Context context, String companyID) {
        PreferencesUtils.putString(context, PreKey.COMPANY_ID, companyID);
    }

    public final static String getCompanyID(Context context) {
        return PreferencesUtils.getString(context, PreKey.COMPANY_ID, "");
    }

    public final static void setCompanyTypeID(Context context, String companyID) {
        PreferencesUtils.putString(context, PreKey.COMPANY_TYPE_ID, companyID);
    }

    public final static String getCompanyTypeID(Context context) {
        return PreferencesUtils.getString(context, PreKey.COMPANY_TYPE_ID, "");
    }

    public final static void setCityName(Context context, String cityName) {
        PreferencesUtils.putString(context, PreKey.USER_CITY_NAME, cityName);
    }

    public final static String getCityName(Context context) {
        return PreferencesUtils.getString(context, PreKey.USER_CITY_NAME, "广州市");
    }

    public final static void setCity(Context context, int city) {
        PreferencesUtils.putInt(context, PreKey.USER_CITY, city);
    }

    public final static int getCity(Context context) {
        return PreferencesUtils.getInt(context, PreKey.USER_CITY, -1);
    }


    /**
     * 保存用户信息
     *
     * @param user
     * @param context
     */
    public final static void saveUser(Context context, User user) {
        PreferencesUtils.putInt(context, PreKey.USER_SALESID, user.getSalesID());
        if (user.getCity() instanceof Integer) {
            PreferencesUtils.putInt(context, PreKey.USER_CITY, (Integer) user.getCity());
        }
        PreferencesUtils.putString(context, PreKey.USER_CITY_NAME, user.getCityName());
        PreferencesUtils.putString(context, PreKey.USER_GO_TO_TYPE, user.getGoToType());
    }

    /**
     * 获取用户信息
     *
     * @param context
     */
    public final static User getUser(Context context) {
        User user = new User(PreferencesUtils.getInt(context, PreKey.USER_SALESID)
                , PreferencesUtils.getInt(context, PreKey.USER_CITY)
                , PreferencesUtils.getString(context, PreKey.USER_CITY_NAME, "广州市")
                , PreferencesUtils.getString(context, PreKey.USER_GO_TO_TYPE)
        );

        return user;
    }

    /**
     * 清除用户信息
     *
     * @param context
     */
    public final static void clearUser(Context context) {
        PreferencesUtils.putInt(context, PreKey.USER_SALESID, -1);
        PreferencesUtils.putInt(context, PreKey.USER_CITY, -1);
        PreferencesUtils.putString(context, PreKey.USER_CITY_NAME, "");
        PreferencesUtils.putString(context, PreKey.USER_GO_TO_TYPE, "");
        setLogin(context, false);
    }

    /**
     * 设置api地址类型
     * @param context context
     * @param apiAddressType api地址类型 1、测试环境一 2、测试环境二 3、灰度环境 4、生产环境 其他表示开发地址[debug]
     */
    public static void setApiAddressType(Context context,int apiAddressType){
        PreferencesUtils.putInt(context,PreKey.API_ADDRESS_TYPE_I,apiAddressType);
    }

    /**
     * 获取api地址类型 1、测试环境一 2、测试环境二 3、灰度环境 4、生产环境 其他表示开发地址[debug]
     * @param context context
     */
    public static int getApiAddressType(Context context){
        int type=PreferencesUtils.getInt(context,PreKey.API_ADDRESS_TYPE_I);
        return type==-1?0:type;
    }
}
